<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<c:set var="queryList" value="${pageContext.request.contextPath}/permission/list" />
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">-->
<title><c:out value="${not empty p.id ? '修改权限信息':'新增权限'}" /></title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<jsp:include page="../common/import.jsp"></jsp:include>
<style>
.select2-container--default .select2-selection--single {
	border-color: #d8dde6;
	border-radius: 0;
	height: 32px;
}
</style>
</head>

<body>

       <!-- <div class="iframe-cot pt0" id="smIframe"> -->
       <div class="right-form mt0">
       	<form class="add-form" id="editForm" method="post" action="#" onsubmit="return false;">
          <input id="s_p_id" type="hidden" value="${p.id}">
		<ul class="w100 layer-form">
             
               <li>
                   <label class="ser-label"><em class="text-red mr5">*</em>所属应用：</label>
                   <div class="form-content">
                      <select id="s_p_app" name="appId" class="js-example-basic-single form-control-select" onchange="appChange()" <c:out value="${not empty p.id ?'disabled':'' }"  /> >
                                      <option value=""></option>
										<c:forEach items="${appList}" var="a">
											<option value="${a.code }" <c:if test="${a.code eq p.appId }"  >selected</c:if> >${a.name }</option>
										</c:forEach>
                      </select>
                      <div class="form-tips text-red"></div>
                   </div>
               </li>
               <li>
                   <label class="ser-label"><em class="text-red mr5">*</em>所属菜单：</label>
                   <div class="form-content">
				        <input id="s_menuName" name="menuName" class="inputText" value="${p.menuName }" type="text" readonly placeholder="请选择"  <c:out value="${not empty p.id ?'disabled':'' }" /> />	
                        <div class="form-tips text-red" ></div>
                        <input id="s_menuId" name="menuId"  value="${p.menuId }" type="hidden" />	
                        <%--  <select id="s_p_menu" name="menuId" class="form-control-select" <c:out value="${not empty p.id ?'disabled':'' }" />>
                            
                        </select>  --%>
                        <!-- <input type="text" class="inputText" id="s_p_menu" /> -->
                   </div>
               </li>             
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>权限类型：</label>
                   <div class="form-content">
                   <select id="s_p_type" name="type" class="form-control-select" <c:out value="${not empty p.id ?'disabled':'' }"/> onchange="checkMenu()" > 
                           <option value="">请选择</option>
							<option value='0' <c:out value="${'0' eq p.type?'selected':'' }" /> >菜单权限</option>
							<option value='1' <c:out value="${'1' eq p.type?'selected':'' }" /> >功能权限</option>
                    </select>
                    <div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>权限名称：</label>
                   <div class="form-content clearfix">
                       <input id="s_p_name" name="name" class="inputText" value="${p.name}" type="text">
                   	   <div class="form-tips text-red" ></div>
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5">*</em>权限值：</label>
                   <div class="form-content clearfix">
                   		<input id="s_p_pvalue_old" type="hidden" value="${p.pvalue }" />
                        <input id="s_p_pvalue" name="pvalue" class="inputText" value="${p.pvalue}" type="text" onblur="checkPvalue()">
                        <div id="s_p_pvalue_tip" class="form-tips text-red" ></div>
                        
                   </div>
               </li>
               <li >
                   <label class="ser-label"><em class="text-red mr5"></em>权限描述：</label>
                   <div class="form-content clearfix">
                     <input id="s_p_description" name="description" class="inputText" value="${p.description}" type="text">
                   	 <div class="form-tips text-red" ></div>
                   </div>
               </li>	  
                  
               <li class="w-per">
                   <label class="ser-label">&nbsp;</label>
                   <div class="form-content clearfix">
                       <button class="btn btn-orange" id="savebtn" onclick="save()" ><i class="fa fa-save mr5"></i>保存</button>
                       <button id="cancelBtn" class="btn btn-default" onclick="closeLayerOpen()"><i class="fa fa-reply mr5"></i>返回</button>
                   </div>
               </li>
            </ul>
    	</form>
    </div>
    <!-- </div> -->
      
  <div id="treediv" style='overflow-y:scroll;overflow-x:auto;display:none; position:absolute;width:250px;height:200px;background-color:#fff; border:1px solid #BEC0BF;padding:5px;font-size:12px;'>
	
	<ul id="tree" class="ztree"></ul>
</div>
	
</body>
<script type="text/javascript">

function closeLayerOpen(){
	parent.layer.close(parent.layer.getFrameIndex(window.name));
}


$(function(){
	$("#s_menuName").click(function() {
		loadMenus();
	});
	$(".js-example-basic-single").select2({
		placeholder:"请选择",
		 'language':{
				'noResults':function(){
					return "<p>无符合条件数据</p>";
				}
			},
			escapeMarkup: function (markup) {
		        return markup;
		    }
	});
});



function loadMenus(){
	var divObj = $("#treediv");
	var txtObj = $("#s_menuName");
	var appId = $("#s_p_app").val();
	if(appId != null && appId != ''){
		showDivNoCache(divObj,txtObj,menusetting,'${ctx}/permission/query/menus2/'+appId);
	}
}

function appChange(){
	$("#s_menuId").val('');
	$("#s_menuName").val('');
}
//只要检查菜单功能是否存在，一个菜单只能有一个菜单功能
function checkMenu(){
	var menuId=$("#s_menuId").val();
	var type=$("#s_p_type").val();
	if(menuId != '' && menuId != null && type != null && '0' == type){
		$.post('${ctx}/permission/find/menu/permission/'+menuId,{},function(data){
			if(data.code=='yes'){
				var d = data.data;
				$("#s_p_id").val(d.id);
				$("#s_p_name").val(d.name);
				$("#s_p_description").val(d.description);
				$("#s_p_pvalue").val(d.pvalue);
				$("#s_p_pvalue_old").val(d.pvalue);
			}else{
				$("#s_p_id").val('');
				$("#s_p_name").val('');
				$("#s_p_description").val('');
				$("#s_p_pvalue").val('');
				$("#s_p_pvalue_old").val('');
			}
		},'json');
	}else{
		$("#s_p_id").val('');
		$("#s_p_name").val('');
		$("#s_p_description").val('');
		$("#s_p_pvalue").val('');
		$("#s_p_pvalue_old").val('');
	}
}

var pvalueIsFit = false;
function checkPvalue(){
	if(!$("#s_p_pvalue").valid()){
		return false;
	}
	pvalueIsFit = false;
	
	var pvalue=$.trim($("#s_p_pvalue").val());
	
	var oldPvalue = $.trim($("#s_p_pvalue_old").val());
	var id=$("#s_p_id").val();
	//修改页面
	if(id != null && id!= ''){
		if(pvalue == oldPvalue){
			//没做修改
			pvalueIsFit = true;
			return true;
		}
	}
	
	$.ajax({
		url:'${ctx}/permission/check/pvalue',
		async:false,//用同步
		type:"post",
		data:{'pvalue':pvalue},
		dataType:"json",
		success:function(data){
			if(data.code == 'notexist'){
				pvalueIsFit = true;
				$("#s_p_pvalue_tip").html("");
			}else if(data.code=='exist'){
				pvalueIsFit = false;
				$("#s_p_pvalue_tip").html("<font color='red'>该权限值已存在");	
			}else{
				pvalueIsFit = false;
				$("#s_p_pvalue_tip").html("<font color='red'>系统出错，校验失败");
			}
		},
		error:function(){
			pvalueIsFit = false;
		}
	});
}
//select2失焦校验会失效,独立处理
$("#s_p_app").on("select2:close",function(e){
	console.log("onblur validate");
	console.log("select值："+$("#s_p_app").val());
	$(this).valid();
});


$("#editForm").validate({
	onkeyup : false,
	onsubmit: false,
	onfocusout:  function(element){
	      $(element).valid() ;
				 }, 
	rules : {
		 appId : {
			required : true
		}, 
		menuName : {
			required : true
		},
		type : {
			required : true
		},
		name : {
			required : true,
			maxlength: 50,
			isContainsSpecialChar: true
		},
		pvalue : {
			required : true,
			maxlength: 400
		}, 
		description : {
			
			maxlength:100
		}
	},
	messages : {
		 appId : {
			required : "请完善必填信息"
		}, 
		menuName : {
			required : "请完善必填信息"
		},
		type : {
			required : "请完善必填信息"
		},
		name : {
			required : "请完善必填信息",
			maxlength: "请输入正确的权限名称",
			isContainsSpecialChar: "请输入正确的权限名称"
		},
		 pvalue : {
		   required : "请完善必填信息",
		   maxlength: "请输入正确的权限值"
		}, 
		
		description : {
			maxlength : "请输入正确的描述"
		}
	},
	errorPlacement: function(error, element) {
		
		 $(element).addClass('input-danger');
		 $(element).next("span").addClass('input-danger');//select2变红
		 if(error){    //error存在的时候
			
			$(element).nextAll("div .form-tips").html(error); //showMsg为你自定义的信息显示区域的id。
		 }                            //这里的element是录入数据的对象  
		 
		
	 },
	 success:function(label){
		 console.log("validate success");
		 $(label).parent().prev('.input-danger').removeClass('input-danger');
	 	 $(label).remove();
	 }
});

function save(){
	
	if(!$("#s_p_app").valid() || !$("#s_menuName").valid() || !$("#s_p_type").valid()
			|| !$("#s_p_name").valid() || !$("#s_p_pvalue").valid() || !$("#s_p_description").valid()){
		return false;
	}
	
	
	var id=$("#s_p_id").val();
	var app=$.trim($("#s_p_app").val());
	var menu=$.trim($("#s_menuId").val());
	
	var name=$.trim($("#s_p_name").val());
	var type=$.trim($("#s_p_type").val());
	var description = $("#s_p_description").val();
	
	checkPvalue();
	if(!pvalueIsFit){
	return;
	}
	
	var pvalue=$.trim($("#s_p_pvalue").val());
	var oldPvalue = $("#s_p_pvalue_old").val();
	
	var data={
			permissionId:id,
			permissionName:name,
			description:description,
			permissionType:type,
			menuId:menu,
			pvalue:pvalue,
			oldPvalue:oldPvalue
			};
	
	//防止多次点击
	$("#savebtn").attr('disabled','disabled');
	//alert('save');
 	$.post("${ctx}/permission/save",data,function(result){
		if(result.code=="success"){
			 successTip();
			 
			 if(id == null || id == ''){
				
				 window.parent.jumpPage(1,'',true);
			 }else{
				 
				 window.parent.jumpPage('','',true);
			 }
			 
			 if($("#s_p_id").val()!=''){
				 //修改，修改的话就关闭
				 setInterval("parent.layer.close(parent.layer.getFrameIndex(window.name))",2000);
			 }else{
				// $("#s_p_pvalue").val('');
				 $("#savebtn").removeAttr('disabled');
			 }
			 
		}else{
			errorTip();
			$("#savebtn").removeAttr('disabled');
		}
	},"json"); 
 	
}


</script>
</html>